<template>
  <!-- 接口管理 -->
  <div>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-form :inline="true" :model="queryParam" class="demo-form-inline" size="medium">
        <el-form-item label="接口名称">
          <el-input v-model="queryParam.interfaceName" placeholder="接口名称"></el-input>
        </el-form-item>
        <el-form-item label="版本">
          <el-select v-model="queryParam.version" placeholder="请选择版本" clearable>
            <el-option v-for="item in versionOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" :body-style="{ padding: 10 }">
      <el-table :data="tableData" border style="width: 100%" height="65vh">
        <el-table-column type="index" width="50" label="序号" align="center"> </el-table-column>
        <el-table-column v-for="item of tableProps" :key="item.props" :prop="item.prop" :label="item.title"
          :width="item.width">
          <template #default="scope">
            <span v-if="item.prop === 'interfaceType'">
              <el-tag v-if="scope.row.interfaceType == 0">本地接口</el-tag>
              <el-tag v-if="scope.row.interfaceType == 1">远程接口</el-tag>
              <el-tag v-if="scope.row.interfaceType == 2" type="info">未知接口</el-tag>
            </span>
            <span v-else>{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="">
          <template slot-scope="{ row }">
            <el-button icon="el-icon-edit" link size="mini" @click="handleEdit(row)">编辑</el-button>
            <el-button icon="el-icon-copy-document" link size="mini" @click="handleCopy(row)">复制</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <interfaces-dialog ref="interfacesDialog" @ok="getVersions" />
  </div>
</template>

<script>
import { getList, dropDownVersion } from '@/api/sys/interfaces'
import interfacesDialog from './interfacesDialog.vue'
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Interface',
  components: { interfacesDialog },
  data() {
    return {
      queryParam: {},
      tableData: [],
      tableProps: [
        { title: '接口编码', prop: 'interfaceCode', width: '', checked: true },
        { title: '接口名称', prop: 'interfaceName', width: '', checked: true },
        { title: '版本号', prop: 'version', width: '', checked: true },
        { title: '接口类型', prop: 'interfaceType', width: '', checked: true },
        { title: 'HTTP协议类型', prop: 'httpType', width: '', checked: true },
        { title: '备注', prop: 'note', width: '', checked: true },
      ],
      versionOptions: [],
    }
  },
  mounted() {
    this.getVersions()
  },
  methods: {
    getVersions() {
      dropDownVersion()
        .then((res) => {
          if (res.data.code === 200) {
            this.versionOptions = res.data.data
            this.loadData()
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    onSubmit() {
      this.loadData()
    },
    loadData() {
      getList(this.queryParam)
        .then((res) => {
          if (res.data.code === 200) {
            this.tableData = res.data.data
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    handleEdit(row) {
      this.$refs.interfacesDialog.edit(row)
    },
    handleCopy(row) {
      this.$refs.interfacesDialog.copy(row)
    },
  },
}
</script>

<style lang="less" scoped>
.demo-form-inline {
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
